/**
 * Created by xiongxin on 2017/8/21.
 */
(function () {

    //计数的背景幕
    var bgColor = 'rgba(0,0,0,0.7)';
    var XeLoading = {
        sum: 0,
        show: function () {
            this.sum++;
            if (this.sum == 1) {
                this.append();
            }
        },
        hide: function () {
            if (this.sum > 0) {
                this.sum--;
            }

            if (this.sum == 0) {
                this.remove();
            }
        },
        append: (function () {
            var body = document.getElementsByTagName('body')[0];
            var loadDiv = document.createElement('div');

            loadDiv.id = 'Xe___Loading';
            loadDiv.style.position = 'fixed';
            loadDiv.style.width = '100%';
            loadDiv.style.height = '100%';
            loadDiv.style.left = '0px';
            loadDiv.style.right = '0px';
            loadDiv.style.bottom = '0px';
            loadDiv.style.top = '0px';
            loadDiv.style.backgroundColor = bgColor;
            loadDiv.style.zIndex = '1001';

            return function () {
                body.appendChild(loadDiv);
            }
        })(),
        remove: function () {
            var body = document.getElementsByTagName('body')[0];
            var loadDiv = document.getElementById('Xe___Loading');

            if (loadDiv) body.removeChild(loadDiv);
        },
        clear: function () {
            this.sum = 0;
            this.hide();
        }
    };

    window.bgPop = XeLoading;
})();

//loading请求加载图片

(function () {
    var bgColor = 'rgba(0,0,0,0.5)';
    var loadPop = {};

    function extend(o, k) {
        for (var x in k) {
            o[x] = k[x];
        }
        return o;
    }

    loadPop = extend(loadPop, bgPop);

    loadPop.append = (function () {

        var basePathFun = function () {
            var basePath = "";
            var h = window.location.protocol;
            var u = window.location.hostname;
            var p = window.location.port;
            if (p && p != "") {
                p = ":" + p;
            }
            basePath = h + "//" + u + p+'/';
            return basePath;
        };

        var body = document.getElementsByTagName('body')[0];
        var loadDiv = document.createElement('div');
        var imgDiv = document.createElement('div');
        var oImg = document.createElement('img');
        oImg.src = 'https://cloud.bankofchina.com/cq/xinyi/static/xinyiApp/img/loading.gif';
        //oImg.src = basePathFun()+'static/xinyiApp/img/loading.gif';
        oImg.style.width = '1.5rem';
        imgDiv.className = 'dbs_load_img';
        loadDiv.appendChild(imgDiv);
        imgDiv.appendChild(oImg);

        loadDiv.id = 'Xe___Loading2';
        loadDiv.style.position = 'fixed';
        loadDiv.style.left = '0px';
        loadDiv.style.right = '0px';
        loadDiv.style.bottom = '0px';
        loadDiv.style.top = '0px';
        loadDiv.style.zIndex = '10000';
        loadDiv.style.backgroundColor = bgColor;


        oImg.style.textAlign = 'center';

        return function () {
            body.appendChild(loadDiv);
        }
    })();
    loadPop.remove = function () {
        var body = document.getElementsByTagName('body')[0];
        var loadDiv = document.getElementById('Xe___Loading2');

        if (loadDiv) body.removeChild(loadDiv);
    };

    window.loadPop = loadPop;
})();

(function () {
    var body = document.getElementsByTagName('body')[0];
    var oDiv = document.createElement('div');
    var p = document.createElement('p');
    var oDiv2 = document.createElement('div');
    var oDiv2Child = document.createElement('div');   //创建oDiv2的子对象
    var btnDiv = document.createElement('div');
    var flag = false;

    oDiv.style.zIndex = '10000';
    oDiv.style.position = 'absolute';
    oDiv.style.width = '2.27rem';
    // oDiv.style.height = 'auto';
    oDiv.style.height = '1.33rem';
    oDiv.style.backgroundColor = 'white';
    // oDiv.style.left = '50%';
    oDiv.style.left = 0;
    // oDiv.style.top = '40%';
    oDiv.style.top = 0;
    oDiv.style.bottom = 0;
    oDiv.style.right = 0;
    oDiv.style.margin = 'auto';
    // oDiv.style.marginLeft = '-1.13rem';
    oDiv.style.borderRadius = '0.05rem';
    oDiv.style.overflow = 'hidden';
    oDiv.style.backgroundClip = 'padding-box';

    p.style.textAlign = 'center';
    p.style.fontSize = '0.18rem';
    p.style.height = '0.34rem';
    p.style.lineHeight = '0.34rem';
    p.style.borderBottom = '1px solid #dedfe0';
    p.style.color = '#333';
    p.style.margin = 0;

    
    oDiv2.style.wordBreak = 'break-word';
    oDiv2.style.minHeight = '0.6rem';
    oDiv2.style.overflow = 'hidden';
    oDiv2.style.fontSize = '0.13rem';
    // oDiv2.style.lineHeight = '0.6rem';
    oDiv2.style.textAlign = 'center';
    oDiv2.style.padding = '0.1rem';
    oDiv2.style.display = 'table';
    oDiv2.style.color = '#8e8e8e';
    oDiv2.style.width = '100%';
    oDiv2.className = 'dbsDiv';

    oDiv2Child.style.display = 'table-cell';
    oDiv2Child.style.verticalAlign = 'middle';



    btnDiv.style.width = '100%';
    // btnDiv.style.overflow = 'hidden';
    btnDiv.style.textAlign = 'center';
    btnDiv.style.marginBottom = '0.18rem';
    btnDiv.className = 'dbsBtnDiv';

    oDiv.appendChild(p);
    oDiv.appendChild(oDiv2);
    oDiv2.appendChild(oDiv2Child);
    oDiv.appendChild(btnDiv);
    var DbsAlert = {
        show: function (o) {
            var options = {
                text: '出错啦',
                onTap: null,
                title: '温馨提示',
                btnText: '确定',
                onClear: null
            };
            var that = this;
            if (typeof o === 'string') {
                options.text = o;
            } else {
                options = extend(options, o);
            }
            
            p.innerText = options.title;
            // oDiv2.innerText = options.text;
            oDiv2Child.innerText = options.text;


            if (!options.clearBtnText) {
                btnDiv.innerHTML = '<button style="width:1.78rem;height:0.24rem;border:1px solid #ff4444;color: #ff4444;font-size:0.13rem;background-color:white;border-radius:0.02rem;">' + options.btnText + '</button>';
            } else {
                btnDiv.innerHTML = '<button class="dbs_red fl" style="background-color:#8e8e8e;color:#fff;border:none;width:0.69rem;height:0.24rem;font-size:0.13rem;border-radius:0.02rem;">' + options.clearBtnText + '</button>' +
                    '<button class="dbs_red fr" style="background-color:#ff4444;color:#fff;border:none;width:0.69rem;height:0.24rem;font-size:0.13rem;border-radius:0.02rem;">' + options.btnText + '</button>';

            }

            var clearBtn = btnDiv.getElementsByTagName('button')[0];
            var confirmBtn = btnDiv.getElementsByTagName('button')[1];

            clearBtn.onclick = function () {
                if (options.onClear) options.onClear();
                that.hide();
            };

            if (confirmBtn) {
                confirmBtn.onclick = function () {
                    if (options.onTap) options.onTap();
                    that.hide();
                };
            }

            body.appendChild(oDiv);
            this.div = oDiv;
            if (!flag) {
                flag = true;
                bgPop.show();
            }
        },

        hide: function () {
            if (flag) {
                flag = false;
                body.removeChild(oDiv);
                bgPop.hide();
            }
        }
    };

    function extend(a, b) {
        for (var i in b) {
            a[i] = b[i];
        }
        return a;
    }

    window.DbsAlert = DbsAlert;
})();

(function () {
    var showTi = {
        show: function (htmlText, url) {
            var layer=document.createElement("div");
            layer.id="layer";
            var style=
                {
                    background:"#000",
                    position:"absolute",
                    zIndex:10,
                    color: '#fff',
                    opacity: '0.8',
                    borderRadius: '0.1rem',
                    bottom: '2rem',
                    padding: '0.05rem 0.1rem',
                    left: '50%'
                }
            for(var i in style)
                layer.style[i]=style[i];

            if(document.getElementById("layer")==null)
            {
                layer.innerHTML = htmlText;
                document.body.appendChild(layer);
                layer.style.marginLeft = (-layer.clientWidth/200)+'rem';
                setTimeout(function () {
                    "document.body.removeChild(layer)";
                    // self.location(url);
                    if(url){
                        window.location.href=url;
                    }
                },1000)
            }
        }
    }
    window.showTi = showTi;
})();

//手势提示框
(function () {
    var showTiGes = {
        show: function (htmlText) {
            var layer=document.createElement("div");
            layer.id="layer";
            var style=
            {
                background:"#000",
                position:"absolute",
                zIndex:10,
                color: '#fff',
                opacity: '0.8',
                borderRadius: '0.1rem',
                bottom: '2rem',
                padding: '0.05rem 0.1rem',
                left: '50%'
            }
            for(var i in style)
                layer.style[i]=style[i];

            if(document.getElementById("layer")==null)
            {
                layer.innerHTML = htmlText;
                document.body.appendChild(layer);
                layer.style.marginLeft = (-layer.clientWidth/200)+'rem';
                setTimeout("document.body.removeChild(layer)",1000)
            }
        }
    }
    window.showTiGes = showTiGes;
})();

/**
 * number:为需要格式化的数值
 * cent： 为number要保留的小数位
 * isThousand： 为是否需要千分位 0:不需要,1:需要(数值类型);
 * 方法返回结果为格式的字符串,如'1,234,567.00'
 */

function formatNumber(number, cent, isThousand) {
       number = number.toString().replace(/\$|\,/g, '');

        // 1、检查传入数值为数值类型
        if (isNaN(number))
                number = '0';
        // 2、获取符号(正/负数)
        sign = (number == (number = Math.abs(number)));//Math.abs(number)取出number的绝对值

        // 3、把指定的小数位先转换成整数.多余的小数位四舍五入，Math.pow(10, cent)为10的cent次方
        number = Math.floor(number * Math.pow(10, cent) + 0.5);
        // 4、求出小数位数值
        cents = number % Math.pow(10, cent);
        // 5、求出整数位数值
        number = Math.floor(number / Math.pow(10, cent)).toString();
        // 6、把小数位转换成字符串,以便求小数位长度
        cents = cents.toString();

        // 7、补足小数位到指定的位数
        while (cents.length < cent)
                cents = "0" + cents;
        // 8、对整数部分进行千分位格式化.
        if (isThousand) {
                for (var i = 0; i < Math.floor((number.length - (1 + i)) / 3) ; i++)
                    number = number.substring(0, number.length - (4 * i + 3)) + ',' + number.substring(number.length - (4 * i + 3));
             }
        //9、返回格式化转换后的结果
        if (cent > 0)
                return (((sign) ? '' : '-') + '￥' + number + '.' + cents);
        else
            return (((sign) ? '' : '-') + '￥' + number);
}
//将银行卡号中间格式化
function formatCardNum(str) {
    var reg = /^(\d{4})\d+(\d{4})$/;
    return str.replace(reg, "$1****$2");
}
//显示卡号后四位
function formatCardNum_4(str) {
    var reg = /^\d+(\d{4})$/;
    return str.replace(reg, "**** **** ****$1");
}

//获取地址栏的URL对象信息；
function getUrl() {
    var url = window.location.search.slice(1);
    var arr = url.split('&');
    var obj = {};
    for (var i=0;i<arr.length;i++){
        obj[arr[i].split('=')[0]] = arr[i].split('=')[1];
    }
    return obj;
}

//获取数组最小值
Array.prototype.min = function(){
    return Math.min.apply({},this);
};
//元素在数组中的位置
Array.prototype.indexOf = function(el){
    for (var i=0,n=this.length; i<n; i++){
        if (this[i] === el){
            return i;
        }
    }
    return -1;
};


// 加密和解密解决URL上传银行卡敏感信息
function compile_jia(code) {
    var c=String.fromCharCode(code.charCodeAt(0)+code.length);
    for(var i=1;i<code.length;i++){
        c+=String.fromCharCode(code.charCodeAt(i)+code.charCodeAt(i-1));
    }
    return c;
}
function uncompile_jie(code) {
    var c=String.fromCharCode(code.charCodeAt(0)-code.length);
    for(var i=1;i<code.length;i++){
        c+=String.fromCharCode(code.charCodeAt(i)-c.charCodeAt(i-1));
    }
    return c;
}




